# Python list列表实现栈和队列
# python list 实现队列
# 定义一个空列表，当做队列
queue = []
# 向列表中插入元素
queue.insert(0, 1)
queue.insert(0, 2)
queue.insert(0, "hello")
print("queue =", queue)
print("取一个元素：", queue.pop())
print("取一个元素：", queue.pop())
print("取一个元素：", queue.pop())

print()
# python list 实现栈
# 定义一个空 list 当做栈
stack = []
stack.append(1)
stack.append(2)
stack.append("hello")
print("stack =", stack)
print("取一个元素：", stack.pop())
print("取一个元素：", stack.pop())
print("取一个元素：", stack.pop())

print()

# collections 模块实现栈和队列
import collections
queueAndStack = collections.deque()
queueAndStack.append(1)
queueAndStack.append(2)
queueAndStack.append("hello")
print("list(queueAndStack) =", list(queueAndStack))

# 实现队列功能，从队列中取一个元素，根据先进先出原则，这里应输出1
print("queueAndStack.popleft() =", queueAndStack.popleft())
# 实现栈功能，从栈里取一个元素，根据后进先出原则，这里应输出 hello
print("queueAndStack.pop() =", queueAndStack.pop())
# 再次打印列表
print("list(queueAndStack) =", list(queueAndStack))

